﻿using System;
using System.Web;
using DNT.RavenQA.Core.Extensions;

namespace DNT.RavenQA.Web.Infrastructure.Extensions
{
    public static class HttpRequestBaseExtensions
    {

        public static Boolean IsUrlLocalToHost(this HttpRequestBase request, String url)
        {
            if (url.IsNullOrEmpty())
                return false;

            Uri absoluteUri;

            if (Uri.TryCreate(url, UriKind.Absolute, out absoluteUri))
                return String.Equals(request.Url.Host,
                                     absoluteUri.Host,
                                     StringComparison.OrdinalIgnoreCase);

            return !url.StartsWith("http:", StringComparison.OrdinalIgnoreCase) &&
                   !url.StartsWith("https:", StringComparison.OrdinalIgnoreCase) &&
                   Uri.IsWellFormedUriString(url, UriKind.Relative);
        }
    }
}